/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.action.equiposRed;

import com.mapping.DetallesRed;
import com.mapping.Dispositivo;
import com.mapping.Red;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Collection;
import java.util.Iterator;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Ricardo
 */
@Namespace(value = "/")
@Action(value = "consultaEquiposRed", results = {
    @Result(name = "success", location = "/GestionEquiposRed/gestionEquiposRed.jsp")})
public class GestionEquiposRed extends ActionSupport{
    private String idRed;
    private Red red;
    private ERed[] equipos;
    
    @Override
    public String execute() {           
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        red = (Red)em.getReference(Red.class, idRed); 
        Collection<DetallesRed> detallesRedCollection = red.getDetallesRedCollection();
        Iterator<DetallesRed> iterator = detallesRedCollection.iterator();
        equipos = new ERed[detallesRedCollection.size()];
        int i =0;
        while(iterator.hasNext()){
            ERed aux = new ERed();
            DetallesRed deta = iterator.next();            
            Dispositivo disp = deta.getIdDisp();
            aux.setIdDetalleRed(deta.getIdDetalleRed());
            aux.setIdDispositivo(disp.getIdDisp());
            aux.setIp(deta.getIp());
            aux.setMac(deta.getMac());           
            equipos[i]=aux;
            i++;
        }
        em.flush();
        tx.commit();
        em.close();     
        return SUCCESS;
    }  

    public Red getRed() {
        return red;
    }

    public void setIdRed(String idRed) {
        this.idRed = idRed;
    }

    public ERed[] getEquipos() {
        return equipos;
    }        
        
}
